﻿// Copyright 2009 (c) Tom van Dijck

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace Bonehead
{
    [Serializable]
    public class Project
    {
        private string mName = String.Empty;
        private string mExe;
        private string mWorkingfolder;
        private string mArguments;
        private Process mProcess;
        private Profile mProfile;
        private List<Profile> mProfiles = new List<Profile>();

        // events.
        public event EventHandler ProfileExited;

        // methods.
        public Project()
        {
        }

        public Project(string aExe, string aWorkingfolder, string aArguments)
        {
            mExe = aExe;
            mWorkingfolder = aWorkingfolder;
            mArguments = aArguments;
        }

        public override string ToString()
        {
            return Path.GetFileNameWithoutExtension(String.IsNullOrEmpty(mName) ? mExe : mName);
        }

        public bool Start()
        {
            if (String.IsNullOrEmpty(mName))
            {
                MessageBox.Show("Save the project first");
                return false;
            }

            if (!File.Exists(mExe))
            {
                MessageBox.Show("The executable '" + mExe + "' does not exist.");
                return false;
            }

            // create profile.
            mProfile = new Profile(mName, mProfiles.Count);

            // create a process executor
            ProcessStartInfo psi = new ProcessStartInfo(mExe);
            psi.WorkingDirectory = mWorkingfolder;
            psi.Arguments = mArguments;
            psi.EnvironmentVariables["COR_ENABLE_PROFILING"] = "1";
            psi.EnvironmentVariables["COR_PROFILER"] = "{D4EEA2D8-DA55-4202-9DE6-7E7954AD4CD2}";
            psi.EnvironmentVariables["PROFILER_OUTPUT"] = mProfile.OutputPath;
            psi.UseShellExecute = false;

            // run process.
            mProcess = Process.Start(psi);
            if (mProcess != null)
            {
                mProcess.EnableRaisingEvents = true;
                mProcess.Exited += new EventHandler(OnProcessExited);
                return true;
            }
            return false;
        }

        public void Stop()
        {
            if (mProcess != null)
            {
                mProcess.Kill();
            }
        }

        public bool IsProfiling
        {
            get { return mProcess != null; }
        }

        [XmlIgnore]
        public string Name
        {
            get { return mName; }
            set { mName = value; }
        }

        public string Exe
        {
            get { return mExe; }
            set { mExe = value; }
        }

        public string Workingfolder
        {
            get { return mWorkingfolder; }
            set { mWorkingfolder = value; }
        }

        public string Arguments
        {
            get { return mArguments; }
            set { mArguments = value; }
        }

        public List<Profile> Profiles
        {
            get { return mProfiles; }
        }

        private void OnProcessExited(object sender, EventArgs e)
        {
            mProfiles.Add(mProfile);
            mProcess = null;
            mProfile = null;
            if (ProfileExited != null)
            {
                ProfileExited(this, EventArgs.Empty);
            }
        }
    }
}
